#!/usr/bin/env python
# encoding: utf-8

"""
@author: Jython <ijython@qq.com>
@license: Apache Licence 
@file: hdfs.py
@time: 2017/10/16 5:33
"""


import sys,re,pyhdfs

#获取用户列表,生成字典
fs=pyhdfs.HdfsClient(hosts='192.168.0.116')
hdfs_users_file=fs.open('/p/user/user.txt')
hdfs_users={}
uu=hdfs_users_file.readlines()
for u in uu:
	temp_u=u.split(",")
	hdfs_users[temp_u[0]]=temp_u

#配置规则
user_re=re.compile('\?userId=(?P<userid>\d+)')

uuidd={}
for line in sys.stdin:
	line=line.strip()
	#line_arr=map('strip',line.split('\t'))
	line_arr=line.split('\t')
	url=line_arr[1]
	uuid=line_arr[5]

	#过滤掉无用的文件
	if url.startswith('/lmsMessage') or (url.startswith('/resource/') and url.find('.html')==-1 and url.find('userId')==-1) or url.find('favicon.ico')>-1:
		continue

	#配置userid
	userinfo=user_re.search(url)
	if userinfo:
		user_id=userinfo.groupdict()['userid']
		user_info=hdfs_users[str(user_id)]
		uuidd[uuid]=user_id
		#修改状态码
		line_arr[2]='200'
		line_arr.append(user_id)
		line_arr.append(user_info[2].strip())
	else:
		user_id=uuidd.get(uuid)
		if user_id:
			user_info=hdfs_users[str(user_id)]
			line_arr.append(user_id)
			line_arr.append(user_info[2].strip())
	#print "%s\t%s" % ('@'.join(line_arr),1)
	print '\t'.join(line_arr)
			#print '@'.join(line_arr).strip()
			#print line
			#print line
			#print "%s\t%s" % ('@'.join(line_arr), 1)
			#sprint "%s\t%s" % ('@'.join(line_arr), 1)
			#print line
			#print "%s\t%s" % (line, 1)
			#print line

			#print "%s|||%s" % (line, 1)
			#print '------------------'